package com.example.demoback.web;

import com.example.demoback.entity.Item;
import com.example.demoback.entity.Result;
import com.example.demoback.mapper.ItemMapper;
import com.example.demoback.service.ItemService;
import com.example.demoback.service.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("item")
public class ItemAction {


    @Autowired
    ItemService itemService;
    @Autowired
    private ItemMapper itemMapper;

    @RequestMapping("news")
    Object queryByName(String name) throws ServiceException {
        List<Item> items = itemMapper.selectByName(name);
        return null;
    }

    @RequestMapping("save")
    Result save(Item item) {
        try{
            itemService.save(item);
            return new Result(1,"商品添加成功",item);
        }catch (ServiceException e){
            return new Result(0,e.getMessage(),null);
        }
    }

    @RequestMapping("del")
    Result delete(int id) {
        itemMapper.deleteById(id);
        return new Result(1,"删除成功",null);

    }

}
